package com.netease.nie.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.netease.nie.dto.AdminProvinceDto;
import com.netease.nie.dto.AdminProvinceDto.CityProvinceDto;

@Component
@ConfigurationProperties(prefix = "area", locations = "classpath:config/admin-area.properties")
public class AdminAreaProperties {

	private Map<Integer, String> adminAreaMap;
	
	public Map<Integer, String> getAdminAreaMap() {
		return adminAreaMap;
	}

	public void setAdminAreaMap(Map<Integer, String> adminAreaMap) {
		this.adminAreaMap = adminAreaMap;
	}
	
	public List<CityProvinceDto> generateAdminProvinceDtoList() {
		CityProvinceDto adminProvinceDto = null;
		List<CityProvinceDto> adminProvinceDtoList = new ArrayList<CityProvinceDto>();
		Map<Integer, String> adminAreaTreeMap = new TreeMap<>(adminAreaMap);
		Set<Entry<Integer, String>> entrySet = adminAreaTreeMap.entrySet();
		for (Entry<Integer, String> entry : entrySet) {
			int code = entry.getKey();
			String name = entry.getValue();
			if (code % 10000 == 0) {
				adminProvinceDto = new CityProvinceDto();
				adminProvinceDto.setCode(code);
				adminProvinceDto.setName(name);
				adminProvinceDtoList.add(adminProvinceDto);
			}			
		}
		return adminProvinceDtoList;
	}
	
	public List<CityProvinceDto> generateAdminCityDtoList(int provinceCode) {
		CityProvinceDto adminCityDto = null;
		List<CityProvinceDto> adminCityDtoList = new ArrayList<CityProvinceDto>();
		Map<Integer, String> adminAreaTreeMap = new TreeMap<>(adminAreaMap);
		Set<Entry<Integer, String>> entrySet = adminAreaTreeMap.entrySet();
		for (Entry<Integer, String> entry : entrySet) {
			int code = entry.getKey();
			String name = entry.getValue();
			if (code <= provinceCode) {
				continue;
			}else if(code >= provinceCode + 10000){		
				break;
			} else {
				adminCityDto = new CityProvinceDto();
				adminCityDto.setCode(code);
				adminCityDto.setName(name);
				adminCityDtoList.add(adminCityDto);
			}
		}
		return adminCityDtoList;
	}
	
}
